import { getAll, addCategory, deleteCategory, amendCategory, allBook } from '@/api/bookmarkClass'
import { warning, success, destroy } from '@/antd';
import { getTreeData } from '@/lib'
import { cloneDeep } from 'lodash'
export default {
    // 开启命名空间（默认为false）
    namespaced: true,
    actions: {
        asyncGetBookmarkList(context, params = {}) {
            getAll(params).then(data => {
                const originArray = cloneDeep(data.list);
                console.log(originArray);
                context.commit('getBookmarkList', {
                    origin: originArray,
                    treeData: getTreeData(data.list)
                });
            }).catch(err => warning(err))
        },
        asyncAddCategory(context, params) {
            addCategory(params).then(() => {
                destroy()
                success('添加成功')
                context.dispatch('asyncGetBookmarkList')
            }).catch(err => warning(err))
        },
        asyncDeleteCategory(context, params) {
            deleteCategory(params).then(() => {
                destroy()
                success('删除成功')
                    // context.dispatch('asyncGetBookmarkList')
            }).catch(err => warning(err))
        },
        asyncAmendCategory(context, params) {
            amendCategory(params).then(() => {
                destroy()
                success('修改成功')
                context.dispatch('asyncGetBookmarkList')
            }).catch(err => warning(err))
        },
        asyncAllBook(context, params) {
            allBook(params).then((data) => {
                console.log(data);
            }).catch(err => warning(err))
        },
        // asyncGetTreeData(context, params) {
        //     const deepClone = cloneDeep(params)
        //     console.log(deepClone);
        //     getTreeData(params).then(data => {
        //         context.commit('getNewBookmarkList', data)
        //     })
        // }
    },
    mutations: {
        getBookmarkList(state, value) {
            state.sourceData = value.origin;
            state.bookmarkList = value.treeData;
        },
        // getNewBookmarkList(state, value) {
        //     state.newBookmarkList = [{ title: '顶级分类', pid: '0', cid: '0' }, ...value];
        // }
    },
    state: () => ({
        bookmarkList: [],
        sourceData: [],
        // newBookmarkList: []
    }),
};